package cn.ffcs.unm.organization.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;

import cn.ffcs.unm.organization.dao.OrgNmgAnizationDao;
import cn.ffcs.unm.organization.model.Department;
import cn.ffcs.unm.organization.model.OrgRelationship;
import cn.ffcs.unm.organization.model.OrganizationHR;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("orgNmgAnizationDao")
public class OrgNmgAnizationDaoImpl extends BaseDaoImpl implements
		OrgNmgAnizationDao {

	@Override
	public PageInfo queryPageInfoByOrgRelationship(
			OrgRelationship orgRelationship, int currentPage, int pageSize) {

		StringBuilder sb = new StringBuilder();
		sb.append(" select * from VIEW_HI_PSNORG where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != orgRelationship) {
			if (!StrUtil.isEmpty(orgRelationship.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(orgRelationship.getMonthId());
			}
			if (!StrUtil.isEmpty(orgRelationship.getPkPsndoc())) {
				sb.append(" AND pkPsndoc LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(orgRelationship.getPkPsndoc())
						+ "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, OrgRelationship.class);
	}

	@Override
	public PageInfo queryPageInfoByDepartment(Department department,
			int currentPage, int pageSize) {
		StringBuilder sb = new StringBuilder(" select * from VIEW_ORG_DEPT where 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != department) {
			if (!StrUtil.isEmpty(department.getMonthId())) {
				sb.append(" AND MONTHID = ? ");
				params.add(department.getMonthId());
			}
			if (!StrUtil.isEmpty(department.getName())) {
				sb.append(" AND NAME LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(department.getName())
						+ "%");
			}
			if (!StrUtil.isEmpty(department.getCode())) {
				sb.append(" AND CODE LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(department.getCode())
						+ "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, Department.class);
	}

	@Override
	public PageInfo queryPageInfoByOrganizationHR(OrganizationHR organizationHR, int currentPage, int pageSize) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select * from VIEW_ORG_ORGS WHERE 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != organizationHR) {
			if (!StrUtil.isEmpty(organizationHR.getMonthId())) {
				sb.append(" AND monthId = ? ");
				params.add(organizationHR.getMonthId());
			}
			if (!StrUtil.isEmpty(organizationHR.getName())) {
				sb.append(" AND NAME LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(organizationHR.getName())
						+ "%");
			}
			if (!StrUtil.isEmpty(organizationHR.getCode())) {
				sb.append(" AND code LIKE ? ");
				params.add("%"
						+ StringEscapeUtils.escapeSql(organizationHR.getCode())
						+ "%");
			}
		}
		return this.jdbcFindPageInfo(sb.toString(), params, currentPage,
				pageSize, OrganizationHR.class);
	}

}
